package com.dengqi.mymodule.activity.permission;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import androidx.annotation.Nullable;
import io.reactivex.functions.Consumer;

public class RxPermissionActivity extends BaseActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_test);

    }

    public void onBtn1(View view) {
        new RxPermissions(this)
                .requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) {
                        if (permission.granted) {
                            // 用户允许权限
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了权限申请
                        } else {
                            // 用户拒绝，并且选择不再提示,可以引导用户进入权限设置界面开启权限
                            showTipsDialog();
                        }

                        Log.e("---", permission.name + " - " + permission.granted);
                    }
                });

    }


    public void onBtn2(View view) {
        new RxPermissions(this)
                .requestEach(Manifest.permission.WRITE_CALENDAR)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) {
                        if (permission.granted) {
                            // 用户允许权限
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了权限申请
                        } else {
                            // 用户拒绝，并且选择不再提示,可以引导用户进入权限设置界面开启权限
                            showTipsDialog();
                        }

                        Log.e("---", permission.name + " - " + permission.granted);
                    }
                });
    }

    private void showTipsDialog() {
        new AlertDialog.Builder(this)
                .setTitle("提示!")
                .setMessage("未获得使用权限,请在应用权限设置中打开权限")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        toAppDetailSetting();
                    }
                })
                .setNegativeButton("取消", null).create().show();
    }

    private void toAppDetailSetting() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        startActivity(intent);
        finish();
    }
}

